/****************************************************************************
 Copyright (c) 2012-2012 lijiejun
 Copyright (c) 2010-2012 cocos2d-x.org
 Copyright (c) 2008-2010 Ricardo Quesada
 Copyright (c) 2011      Zynga Inc.
 
 http://www.cocos2d-x.org
 
 Permission is hereby granted, free of charge, to any person obtaining a copy
 of this software and associated documentation files (the "Software"), to deal
 in the Software without restriction, including without limitation the rights
 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 copies of the Software, and to permit persons to whom the Software is
 furnished to do so, subject to the following conditions:
 
 The above copyright notice and this permission notice shall be included in
 all copies or substantial portions of the Software.
 
 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 THE SOFTWARE.
 ****************************************************************************/

package org.cocos2d.menu_nodes
{
	import org.cocos2d.basenodes.CCNode;
	import org.cocos2d.cocoa.CCObject;
	import org.cocos2d.support.CCRGBAProtocol;
	import org.cocos2d.support.ccColor3B;

	public class CCMenuItemSprite extends CCMenuItem implements CCRGBAProtocol
	{
		protected var m_pNormalImage : CCNode;
		
		public function get NormalImage() : CCNode
		{
			return m_pNormalImage;
		}
		
		public function set NormalImage(value : CCNode) : void
		{
			m_pNormalImage = value;
		}
		
		protected var m_pSelectedImage : CCNode;
		
		public function get SelectedImage() : CCNode
		{
			return m_pSelectedImage;
		}
		
		public function set SelectedImage(value : CCNode) : void
		{
			m_pSelectedImage = value;
		}
		
		protected var m_pDisabledImage : CCNode;
		
		public function get DisabledImage() : CCNode
		{
			return m_pDisabledImage;
		}
		
		public function set DisabledImage(value : CCNode) : void
		{
			m_pDisabledImage = value;
		}
		
		public function CCMenuItemSprite()
		{
			m_pNormalImage = null;
			m_pSelectedImage = null;
			m_pDisabledImage = null;
		}
		
		public static function itemWithNormalSprite(normalSprite : CCNode, selectedSprite : CCNode, disabledSprite : CCNode = null) : CCMenuItemSprite
		{
			return CCMenuItemSprite.create(normalSprite, selectedSprite, disabledSprite);
		}
		
		public static function itemWithNormalSprite(normalSprite : CCNode, selectedSprite : CCNode, target : CCObject, selector : Function) : CCMenuItemSprite
		{
			return CCMenuItemSprite.create(normalSprite, selectedSprite, target, selector);
		}
		
		public static function itemWithNormalSprite(normalSprite : CCNode, selectedSprite : CCNode, disabledSprite : CCNode, target : CCObject, selector : Function) : CCMenuItemSprite
		{
			return CCMenuItemSprite.create(normalSprite, selectedSprite, disabledSprite, target, selector);
		}
		
		public static function create(normalSprite : CCNode, selectedSprite : CCNode, disabledSprite : CCNode = null) : CCMenuItemSprite
		{
			return CCMenuItemSprite.create(normalSprite, selectedSprite, disabledSprite, null, null);
		}
		
		public static function create(normalSprite : CCNode, selectedSprite : CCNode, target : CCObject, selector : Function) : CCMenuItemSprite
		{
			return CCMenuItemSprite.create(normalSprite, selectedSprite, null, target, selector);
		}
		
		public static function create(normalSprite : CCNode, selectedSprite : CCNode, disabledSprite : CCNode, target : CCObject, selector : Function) : CCMenuItemSprite
		{
			var pRet : CCMenuItemSprite = new CCMenuItemSprite();
			pRet.initWithNormalSprite(normalSprite, selectedSprite, disabledSprite, target, selector);
			return pRet;
		}
		
		public function initWithNormalSprite(normalSprite : CCNode, selectedSprite : CCNode, disabledSprite : CCNode, target : CCObject, selector : Function) : Boolean
		{
			super.initWithTarget(target, selector);
			NormalImage = normalSprite;
			SelectedImage = selectedSprite;
			DisabledImage = disabledSprite;
			
			if (m_pNormalImage)
			{
				ContentSize = m_pNormalImage.ContentSize;
			}
			return true;
		}
		
		public override function set Color(color : ccColor3B) : void
		{
			(m_pNormalImage as CCRGBAProtocol).Color = color;
			
			if (m_pSelectedImage)
			{
				(m_pSelectedImage as CCRGBAProtocol).Color = color;
			}
			
			if (m_pDisabledImage)
			{
				(m_pDisabledImage as CCRGBAProtocol).Color = color;
			}
		}
		
		public override function get Color() : ccColor3B
		{
			return (m_pNormalImage as CCRGBAProtocol).Color;
		}
		
		public override function set Opacity(opacity : int) : void
		{
			(m_pNormalImage as CCRGBAProtocol).Opacity = opacity;
			
			if (m_pSelectedImage)
			{
				(m_pSelectedImage as CCRGBAProtocol).Opacity = opacity;
			}
			
			if (m_pDisabledImage)
			{
				(m_pDisabledImage as CCRGBAProtocol).Opacity = opacity;
			}
		}
		
		public override function get Opacity() : int
		{
			return (m_pNormalImage as CCRGBAProtocol).Opacity;
		}
		
		public function selected() : void
		{
			super.selected();
			
			if (m_pNormalImage)
			{
				if (m_pDisabledImage)
				{
					m_pDisabledImage.Visible = false;
				}
				
				if (m_pSelectedImage)
				{
					m_pNormalImage.Visible = false;
					m_pSelectedImage.Visible = true;
				}
				else
				{
					m_pNormalImage.Visible = true;
				}
			}
		}
		
		public function unselected() : void
		{
			super.unselected();
			
			if (m_pNormalImage)
			{
				m_pNormalImage.Visible = true;
				
				if (m_pSelectedImage)
				{
					m_pSelectedImage.Visible = false;
				}
				
				if (m_pDisabledImage)
				{
					m_pDisabledImage.Visible = false;
				}
			}
		}
		
		public override function set Enabled(bEnabled : Boolean) : void
		{
			if (m_bIsEnabled != bEnabled)
			{
				super.Enabled = bEnabled;
				this.updateImagesVisibility();
			}
		}
		
		public override function set OpacityModifyRGB(bValue : Boolean)
		{
		}
		
		public override function get OpacityModifyRGB() : Boolean
		{
			return false;
		}
		
		protected function updateImagesVisibility() : void
		{
			if (m_bIsEnabled)
			{
				if (m_pNormalImage) m_pNormalImage.Visible = true;
				if (m_pSelectedImage) m_pSelectedImage.Visible = false;
				if (m_pDisabledImage) m_pDisabledImage.Visible = false;
			}
			else
			{
				if (m_pDisabledImage)
				{
					if (m_pNormalImage) m_pNormalImage.Visible = false;
					if (m_pSelectedImage) m_pSelectedImage.Visible = false;
					if (m_pDisabledImage) m_pDisabledImage.Visible = true;
				}
				else
				{
					if (m_pNormalImage) m_pNormalImage.Visible = true;
					if (m_pSelectedImage) m_pSelectedImage.Visible = false;
					if (m_pDisabledImage) m_pDisabledImage.Visible = false;
				}
			}
		}
	}
}